package com.imooc.reader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imooc.reader.mapper.MemberReadStateMapper;
import com.imooc.reader.entity.MemberReadState;
import com.imooc.reader.service.MemberReadStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class MemberReadStateServiceImpl implements MemberReadStateService {

    @Autowired
    private MemberReadStateMapper memberReadStateMapper;

    @Override
    public MemberReadState selectState(Long memberId, Long bookId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("member_id",memberId);
        queryWrapper.eq("book_id",bookId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);

        return memberReadState;
    }

    @Override
    public void updateState(Long bookId, Long memberId, Integer state) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("book_id",bookId);
        queryWrapper.eq("member_id",memberId);
        MemberReadState memberReadState = memberReadStateMapper.selectOne(queryWrapper);
        //没看见过此书
        if (memberReadState==null){
            MemberReadState memberReadState1=new MemberReadState();
            memberReadState1.setBookId(bookId);
            memberReadState1.setMemberId(memberId);
            memberReadState1.setCreateTime(new Date());
            memberReadState1.setReadState(state);
            memberReadStateMapper.insert(memberReadState1);
        }else{
            memberReadState.setReadState(state);
            memberReadStateMapper.updateById(memberReadState);
        }

    }
}
